package com.nameli.admin.controller;

import com.github.pagehelper.PageInfo;
import com.nameli.SIM.bean.query.DepartQuery;
import com.nameli.SIM.bean.vo.DepartmentVO;
import com.nameli.SIM.bean.vo.MajorInfoVO;
import com.nameli.SIM.util.EmptyUtil;
import com.nameli.SIM.util.ReturnStatus;
import com.nameli.SIM.util.response.JsonResult;
import com.nameli.admin.service.DepartMentService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author NAME-L
 * @Description 学部操作 控制层
 * @className DepartMentController
 * @date 2021-11-12 17:48:52
 */
@RestController
@CrossOrigin
@RequestMapping("/depart")
public class DepartMentController {

    private static final Log log = LogFactory.getLog(DepartMentController.class);

    @Autowired
    private DepartMentService departMentService;

    /**
     * 查询所有专业信息
     * @param pageIndex
     * @param pageSize
     * @return MajorInfoVO
     */
    @GetMapping("/index")
    public JsonResult  queryAllByPage(@RequestParam("pageIndex") int pageIndex , @RequestParam("pageSize") int pageSize){
        log.info("enter into -----------> DepartMentController - queryAllByPage");
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息
        JsonResult  jsonResult = null;
        if( EmptyUtil.isNotEmpty(pageIndex) && EmptyUtil.isNotEmpty(pageSize)){
            log.info("pageIndex:" + pageIndex + "\t" + "pageSize:"+pageSize);
            log.info("check -----------> pageIndex & pageSize 不为空");
            PageInfo<DepartmentVO> returnList = departMentService.queryAllDepart(pageIndex , pageSize);
            errorCode = ReturnStatus.SUCCESS;
            errorMsg = "查询成功";
            jsonResult = JsonResult.returnMethod(errorCode, errorMsg, returnList);
        }else{
            log.info("check -----------> pageIndex & pageSize 为空");
            errorCode = ReturnStatus.DO_FAILED;
            errorMsg = "查询失败";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        }
        return jsonResult;
    }

    @GetMapping("/info/condition")
    public JsonResult queryByCondition(DepartQuery departQuery, @RequestParam(value = "pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize) {
        log.info("enter into -----------> DepartMentController - queryAllByPage");
        JsonResult jsonResult = null;
        try {
            if (EmptyUtil.isNotEmpty(departQuery) && EmptyUtil.isNotEmpty(pageIndex) && EmptyUtil.isNotEmpty(pageSize)) {
                log.info("pageIndex:" + pageIndex + "\t" + "pageSize:"+pageSize);
                log.info("check -----------> pageIndex & pageSize 不为空");
                log.info("departQuery查询条件：" + departQuery);
                PageInfo<DepartmentVO> returnList = departMentService.queryByCondition(departQuery, pageIndex, pageSize);
                jsonResult = JsonResult.querySuccessFul(returnList);
            }else {
                jsonResult = JsonResult.delFailed();
            }
        } catch (Exception e) {
            log.error("e:" + e);
            jsonResult = JsonResult.systemError();
        }
        return jsonResult;
    }
}
